package org.zfes.snowier.common.monitor.config.ws.mock;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.zfes.snowier.common.monitor.config.ws.MonitorWebSocketHandler;
import org.zfes.snowier.common.monitor.config.ws.dto.JSONMSG;
import org.zfes.snowier.common.monitor.config.ws.dto.TransAmont;
import org.zfes.snowier.common.monitor.config.ws.dto.TransOrders;
import org.zfes.snowier.common.monitor.config.ws.dto.TransRuntimeCount;

import com.alibaba.fastjson.JSON;
@Component
public class TranAmontMock {

	private ScheduledExecutorService scheduledThreadPool ;
	@Autowired
	MonitorWebSocketHandler monitorWebSocketHandler;
	
	
	public TranAmontMock() {
		super();
		this.scheduledThreadPool = Executors.newScheduledThreadPool(5);
		//startTranAmontMock();
		//startTransOrdersMock();
		//startTransRuntimeCountMock();
	}


	private static final long collectPeriod=3;
	
	static int amont=1;
	
	public  void startTranAmontMock() {
		scheduledThreadPool.scheduleAtFixedRate(()->{
			amont++;
			JSONMSG msg=new JSONMSG(200).put("transamont",new TransAmont(amont,BigDecimal.valueOf(amont*1000)));
			
			monitorWebSocketHandler.broadcast(new TextMessage(JSON.toJSONString(msg.setType(1))));
			//System.out.println("-startTranAmontMock---"+ msg);
		}, 1L,collectPeriod, TimeUnit.SECONDS);
	

	}
	
	static int amont2=1;
	
	public  void startTransOrdersMock() {
		scheduledThreadPool.scheduleAtFixedRate(()->{
			
			TransOrders transOrders=new TransOrders( "门诊", "银行卡",  amont2+"N112410000120181203175300",BigDecimal.valueOf(amont2*100)) ;
			amont2++;
			JSONMSG msg=new JSONMSG(200).put("transOrders",transOrders);
			
			monitorWebSocketHandler.broadcast(new TextMessage(JSON.toJSONString(msg.setType(2))));
			//System.out.println("-startTransOrdersMock---"+ msg);
		}, 0L,1, TimeUnit.SECONDS);
	

	}
	
	
	
	
	
	static int amont3=1;
	
	public  void startTransRuntimeCountMock() {
		scheduledThreadPool.scheduleAtFixedRate(()->{
			
			String acqTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			
			JSONMSG msg=new JSONMSG(200).put("transRuntimeCount",new TransRuntimeCount(acqTime,(int)(1+Math.random()*(10-1+1))));
			
			monitorWebSocketHandler.broadcast(new TextMessage(JSON.toJSONString(msg.setType(3))));
			
		//	System.out.println("-startTransRuntimeCountMock---"+ msg);
			
		}, 1L,collectPeriod, TimeUnit.SECONDS);
	

	}

}
